//
//  NavigationBar.swift
//  QFTZ
//
//  Created by ZWJY on 2021/9/18.
//  Copyright © 2021 iOSCoderZhao. All rights reserved.
//

import UIKit
import SnapKit

/// 主题色
enum NavigationBarType {
    case gray  // 默认灰色
    case green //主题绿色
    case white // 主题白色
    case orange // 主题黄色
    case clear // 主题透明
}

/// 自定义导航条
class NavigationBar: UIView {
    /// btn高度
    let comH: CGFloat = 44.0
    /// btn最大宽度
    let maxWidth: CGFloat = 120.0
    /// 间距
    let itemMargin: CGFloat = 15
    //主题灰色颜色
    let grayColor = UIColor.init(hexString: "#eeeeee")
    /// 主题白色
    let whiteColor = UIColor.color(light: .white, dark: .black)
    /// 主题绿色
    let greenColor = UIColor.green
    /// 主题橙色
    let orangeColor = UIColor.orange
    /// 分割线颜色
    let lineColor = UIColor.init(hexString: "#DDDDDD")

    /// 标题，返回按钮主题色
    var tinColor: UIColor = .black {
        didSet{
            if let titleView = titleView as? UILabel {
                titleView.textColor = tinColor
            }

            if let leftView = leftItem as? UIButton {
                leftView.setTitleColor(tinColor, for: .normal)
            }
        }
    }

    /// 设置导航条主题背景色
    var type: NavigationBarType = .gray {
        didSet {
            switch type {
            case .clear:
                statusView.backgroundColor = .clear
                navigationView.backgroundColor = .clear
                bottomLine.backgroundColor = .clear
                tinColor = .white
                if let vc = self.pg_viewController() as? BaseViewController {
                    vc.barStyle = .lightContent
                    vc.setNeedsStatusBarAppearanceUpdate()
                }
            case .gray:
                statusView.backgroundColor = grayColor
                navigationView.backgroundColor = grayColor
                bottomLine.backgroundColor = lineColor
                tinColor = .black
                if let vc = self.pg_viewController() as? BaseViewController {
                    vc.barStyle = .default
                    vc.setNeedsStatusBarAppearanceUpdate()
                }
                break
            case .white:
                statusView.backgroundColor = whiteColor
                navigationView.backgroundColor = whiteColor
                bottomLine.backgroundColor = lineColor
                tinColor = UIColor.color(light: .black, dark: .white)
                if let vc = self.pg_viewController() as? BaseViewController {
                    vc.barStyle = .default
                    vc.setNeedsStatusBarAppearanceUpdate()
                }
                break
            case .green:
                statusView.backgroundColor = greenColor
                navigationView.backgroundColor = greenColor
                bottomLine.backgroundColor = lineColor
                tinColor = .white
                if let vc = self.pg_viewController() as? BaseViewController {
                    vc.barStyle = .lightContent
                    vc.setNeedsStatusBarAppearanceUpdate()
                }
                break
            case .orange:
                statusView.backgroundColor = orangeColor
                navigationView.backgroundColor = orangeColor
                bottomLine.backgroundColor = lineColor
                tinColor = .white
                if let vc = self.pg_viewController() as? BaseViewController {
                    vc.barStyle = .lightContent
                    vc.setNeedsStatusBarAppearanceUpdate()
                }
                break
            }
        }
    }

    /// 添加左侧返回
    var leftItem: UIView? {
        didSet {
            guard let leftItem = leftItem else { return }
            self.addSubview(leftItem)
            layoutSubviews()
        }
    }

    /// 添加右侧
    var rightItem: UIView? {
        didSet {
            guard let rightItem = rightItem else { return }
            rightItems = [rightItem]
        }
    }

    /// 添加右侧多个
    var rightItems: [UIView]? {
        didSet{
            guard let rightItems = rightItems else { return }
            rightItems.forEach { self.addSubview($0) }
            layoutSubviews()
        }
    }

    /// 中间View
    var titleView: UIView? {
        didSet{
            guard let titleView = titleView else { return }
            self.addSubview(titleView)
            layoutSubviews()
            if let titleView = titleView as? UILabel {
                titleView.textAlignment = .center
            }
        }
    }

    /// 是否存在分割线
    var hasBottomLine: Bool = true {
        didSet{
            bottomLine.isHidden = !hasBottomLine
        }
    }

    /// 状态栏的图片
    var statusImage: UIImage? {
        didSet{
            statusView.image = statusImage
        }
    }

    /// 导航条的图片
    var navigationImage: UIImage? {
        didSet{
            navigationView.image = navigationImage
        }
    }

    /// 是否高斯模糊
    var isTranslucent: Bool = false {
        didSet {
            toolbar.isHidden = !isTranslucent
        }
    }

    /// 状态栏
    private lazy var statusView: UIImageView = {
        let view = UIImageView.init(img: nil, contentMode: .scaleAspectFill)
        return view
    }()
    /// 导航条
    private lazy var navigationView: UIImageView = {
        let view = UIImageView.init(img: nil, contentMode: .scaleAspectFill)
        return view
    }()
    /// 高斯模糊
    private lazy var toolbar: UIToolbar = {
        let bar = UIToolbar.init()
        bar.isTranslucent = true
        bar.isHidden = isTranslucent
        return bar
    }()
    /// 分割线
    private lazy var bottomLine: UIView = {
        let view = UIView.init()
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addViews()
        addContraints()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: - UI
extension NavigationBar {

    private func addViews() {
        self.addSubview(statusView)
        self.addSubview(navigationView)
        self.addSubview(toolbar)
        self.addSubview(bottomLine)
    }

    private func addContraints() {
        statusView.snp.makeConstraints { make  in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(UIDevice.statusHeight)
        }

        navigationView.snp.makeConstraints { make  in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(statusView.snp.bottom)
        }

        toolbar.snp.makeConstraints { make  in
            make.edges.equalToSuperview()
        }

        bottomLine.snp.makeConstraints { make  in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // 调整分割线
        self.bringSubview(toFront: bottomLine)

        // 存在左侧按钮，调整返回按钮位置
        if let leftItem = leftItem {
            let itemW: CGFloat = leftItem.frame.size.width > 0 ? leftItem.frame.size.width : comH
            let itemH: CGFloat = leftItem.frame.size.height > 0 ? leftItem.frame.size.height : comH
            leftItem.snp.remakeConstraints { make in
                make.left.equalTo(itemMargin)
                make.centerY.equalTo(navigationView)
                make.height.equalTo(itemH)
                make.width.equalTo(itemW)
            }
        }

        //存在右侧按钮 调整位置
        if let rightItems = rightItems {
            var rightMargin = itemMargin
            rightItems.forEach { view in
                let itemW: CGFloat = view.frame.size.width > 0 ? view.frame.size.width : comH
                let itemH: CGFloat = view.frame.size.height > 0 ? view.frame.size.height : comH
                view.snp.remakeConstraints { make  in
                    make.right.equalTo(-rightMargin)
                    make.centerY.equalTo(navigationView)
                    make.height.equalTo(itemH)
                    make.width.equalTo(itemW)
                }
                rightMargin += itemW
            }
        }

        // 存在中间view 调整位置
        if let titleView = titleView {
            // 获取左侧 宽度
            let leftWidth: CGFloat = leftItem?.pg_maxX ?? comH
            if let rightItems = rightItems, !rightItems.isEmpty{
                // 获取右侧 宽度
                let rightWidth: CGFloat = UIDevice.width - (rightItems.last?.pg_x ?? comH)
                let maxMargin =  leftWidth > rightWidth ? leftWidth : rightWidth
                let maxWidth: CGFloat = UIDevice.width - (maxMargin) * 2.0 // maxMargin + itemMargin

                let viewWidth = titleView.pg_width > 0 ? titleView.pg_width : maxWidth
                let viewHeight = titleView.pg_height > 0 ? titleView.pg_height : comH

                titleView.snp.remakeConstraints { make  in
                    make.center.equalTo(navigationView)
                    make.width.equalTo(viewWidth)
                    make.height.equalTo(viewHeight)
                }
            }else{
                let maxWidth: CGFloat = UIDevice.width - (leftWidth + itemMargin) * 2.0
                let viewWidth = titleView.pg_width > 0 ? titleView.pg_width : maxWidth
                let viewHeight = titleView.pg_height > 0 ? titleView.pg_height : comH

                titleView.snp.remakeConstraints { make  in
                    make.center.equalTo(navigationView)
                    make.width.equalTo(viewWidth)
                    make.height.equalTo(viewHeight)
                }
            }
        }
    }
}
